<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>grain2</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="grain.html" title="grain" />
    <link rel="next" href="grain3.html" title="grain3" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">grain2</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="grain.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="grain3.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="grain2"></a>
      <div class="titlepage"></div>
      <a id="IndexGrain2" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">grain2</span>
        </h2>
        <p>grain2 — 
      Easy-to-use granular synthesis texture generator.
          </p>
      </div>
      <div class="refsect1">
        <a id="idp113362624"></a>
        <h2>Description</h2>
        <p>
      Generate granular synthesis textures. <span class="emphasis"><em>grain2</em></span> is simpler to use, but <a class="link" href="grain3.html" title="grain3"><em class="citetitle">grain3</em></a> offers more control.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp113365296"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">ares <span class="command"><strong>grain2</strong></span> kcps, kfmd, kgdur, iovrlp, kfn, iwfn [, irpow] \
      [, iseed] [, imode]</pre>
      </div>
      <div class="refsect1">
        <a id="idp113367664"></a>
        <h2>Initialization</h2>
        <p>
      <span class="emphasis"><em>iovrlp</em></span> -- (fixed) number of overlapping grains.
    </p>
        <p>
      <span class="emphasis"><em>iwfn</em></span> -- function table containing window waveform (Use <a class="link" href="GEN20.html" title="GEN20">GEN20</a> to calculate <span class="emphasis"><em>iwfn</em></span>).
    </p>
        <p>
      <span class="emphasis"><em>irpow</em></span> (optional, default=0) -- this value controls the distribution of grain frequency variation.  If <span class="emphasis"><em>irpow</em></span> is positive, the random distribution (<span class="emphasis"><em>x</em></span> is in the range -1 to 1) is </p>
        <div class="literallayout">
          <p>abs(x) ^ ((1 / irpow) - 1);</p>
        </div>
        <p> for negative <span class="emphasis"><em>irpow</em></span> values, it is </p>
        <div class="literallayout">
          <p>(1 - abs(x)) ^ ((-1 / irpow) - 1)</p>
        </div>
        <p>Setting <span class="emphasis"><em>irpow</em></span> to -1, 0, or 1 will result in uniform distribution (this is also faster to calculate). The image below shows some examples for <span class="emphasis"><em>irpow</em></span>. The default value of <span class="emphasis"><em>irpow</em></span> is 0.
    </p>
        <p>
      </p>
        <div class="mediaobject">
          <img src="images/grain2_rand-448x289.png" alt="[A graph of distributions for different values of irpow.]" />
          <div class="caption">
            <p>A graph of distributions for different values of <span class="emphasis"><em>irpow</em></span>.</p>
          </div>
        </div>
        <p>
    </p>
        <p>
      <span class="emphasis"><em>iseed</em></span> (optional, default=0) -- seed value for random number generator (positive integer in the range 1 to 2147483646 (2<sup>31</sup> - 2)). Zero or negative value seeds from current time (this is also the default).
    </p>
        <p>
      <span class="emphasis"><em>imode</em></span> (optional default=0) -- sum of the following values:
      </p>
        <div class="itemizedlist">
          <ul class="itemizedlist" style="list-style-type: disc; ">
            <li class="listitem">
              <p><span class="emphasis"><em>8:</em></span> interpolate window waveform (slower).</p>
            </li>
            <li class="listitem">
              <p><span class="emphasis"><em>4:</em></span> do not interpolate grain waveform (fast, but lower quality).</p>
            </li>
            <li class="listitem">
              <p><span class="emphasis"><em>2:</em></span> grain frequency is continuously modified by <span class="emphasis"><em>kcps</em></span> and <span class="emphasis"><em>kfmd</em></span> (by default, each grain keeps the frequency it was launched with). This may be slower at high control rates.</p>
            </li>
            <li class="listitem">
              <p><span class="emphasis"><em>1:</em></span> skip initialization.</p>
            </li>
          </ul>
        </div>
        <p>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp113389152"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>ares</em></span> -- output signal.
    </p>
        <p>
      <span class="emphasis"><em>kcps</em></span> -- grain frequency in Hz.
    </p>
        <p>
      <span class="emphasis"><em>kfmd</em></span> -- random variation (bipolar) in grain frequency in Hz.
    </p>
        <p>
      <span class="emphasis"><em>kgdur</em></span> -- grain duration in seconds. <span class="emphasis"><em>kgdur</em></span> also controls the duration of already active grains (actually the speed at which the window function is read). This behavior does not depend on the <span class="emphasis"><em>imode</em></span> flags.
    </p>
        <p>
      <span class="emphasis"><em>kfn</em></span> -- function table containing grain waveform. Table number can be changed at k-rate (this is useful to select from a set of band-limited tables generated by <a class="link" href="GEN30.html" title="GEN30">GEN30</a>, to avoid aliasing).
    </p>
        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
          <table border="0" summary="Note: Note">
            <tr>
              <td rowspan="2" align="center" valign="top" width="25">
                <img alt="[Note]" src="images/note.png" />
              </td>
              <th align="left">Note</th>
            </tr>
            <tr>
              <td align="left" valign="top">
                <p>
        <span class="emphasis"><em>grain2</em></span> internally uses the same random number generator as <span class="emphasis"><em>rnd31</em></span>. So reading <a class="link" href="rnd31.html" title="rnd31"><em class="citetitle">its documentation</em></a> is also recommended.
      </p>
              </td>
            </tr>
          </table>
        </div>
      </div>
      <div class="refsect1">
        <a id="idp113399824"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the grain2 opcode. It uses the file <a class="ulink" href="examples/grain2.csd" target="_top"><em class="citetitle">grain2.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp113401680"></a>
          <p class="title">
            <strong>Example 358. Example of the grain2 opcode.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in    No messages</span>
-odac           -iadc     -d     <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o grain2.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="ohdr">sr</span>	<span class="op">=</span>  48000
<span class="ohdr">kr</span>	<span class="op">=</span>  750
<span class="ohdr">ksmps</span>	<span class="op">=</span>  64
<span class="ohdr">nchnls</span>	<span class="op">=</span>  2

<span class="op">/</span><span class="op">*</span> square wave <span class="op">*</span><span class="op">/</span>
i_	<span class="ohdr">ftgen</span> 1, 0, 4096, 7, 1, 2048, 1, 0, <span class="op">-</span>1, 2048, <span class="op">-</span>1
<span class="op">/</span><span class="op">*</span> window <span class="op">*</span><span class="op">/</span>
i_	<span class="ohdr">ftgen</span> 2, 0, 16384, 7, 0, 4096, 1, 4096, 0.3333, 8192, 0
<span class="op">/</span><span class="op">*</span> sine wave <span class="op">*</span><span class="op">/</span>
i_	<span class="ohdr">ftgen</span> 3, 0, 1024, 10, 1
<span class="op">/</span><span class="op">*</span> room parameters <span class="op">*</span><span class="op">/</span>
i_	<span class="ohdr">ftgen</span> 7, 0, 64, <span class="op">-</span>2, 4, 50, <span class="op">-</span>1, <span class="op">-</span>1, <span class="op">-</span>1, 11,			\
			    1, 26.833, 0.05, 0.85, 10000, 0.8, 0.5, 2,	\
			    1,  1.753, 0.05, 0.85,  5000, 0.8, 0.5, 2,	\
			    1, 39.451, 0.05, 0.85,  7000, 0.8, 0.5, 2,	\
			    1, 33.503, 0.05, 0.85,  7000, 0.8, 0.5, 2,	\
			    1, 36.151, 0.05, 0.85,  7000, 0.8, 0.5, 2,	\
			    1, 29.633, 0.05, 0.85,  7000, 0.8, 0.5, 2

ga01	<span class="opc">init</span> 0

<span class="op">/</span><span class="op">*</span> generate bandlimited square waves <span class="op">*</span><span class="op">/</span>

i0	<span class="op">=</span>  0
<span class="olabel">loop1</span><span class="op">:</span>
imaxh	<span class="op">=</span>  <span class="ohdr">sr</span> <span class="op">/</span> (2 <span class="op">*</span> 440.0 <span class="op">*</span> <span class="opc">exp</span>(<span class="opc">log</span>(2.0) <span class="op">*</span> (i0 <span class="op">-</span> 69) <span class="op">/</span> 12))
i_	<span class="ohdr">ftgen</span> i0 <span class="op">+</span> 256, 0, 4096, <span class="op">-</span>30, 1, 1, imaxh
i0	<span class="op">=</span>  i0 <span class="op">+</span> 1
	<span class="octrl">if</span> (i0 <span class="op">&lt;</span> 127.5) <span class="octrl">igoto</span> <span class="olabel">loop1</span>

	<span class="oblock">instr</span> 1

p3	<span class="op">=</span>  p3 <span class="op">+</span> 0.2

<span class="op">/</span><span class="op">*</span> note velocity <span class="op">*</span><span class="op">/</span>
iamp	<span class="op">=</span>  0.0039 <span class="op">+</span> p5 <span class="op">*</span> p5 <span class="op">/</span> 16192
<span class="op">/</span><span class="op">*</span> <span class="opc">vibrato</span> <span class="op">*</span><span class="op">/</span>
kcps	<span class="opc">oscili</span> 1, 8, 3
kenv	<span class="opc">linseg</span> 0, 0.05, 0, 0.1, 1, 1, 1
<span class="op">/</span><span class="op">*</span> frequency <span class="op">*</span><span class="op">/</span>
kcps	<span class="op">=</span>  (kcps <span class="op">*</span> kenv <span class="op">*</span> 0.01 <span class="op">+</span> 1) <span class="op">*</span> 440 <span class="op">*</span> <span class="opc">exp</span>(<span class="opc">log</span>(2) <span class="op">*</span> (p4 <span class="op">-</span> 69) <span class="op">/</span> 12)
<span class="op">/</span><span class="op">*</span> <span class="opc">grain</span> ftable <span class="op">*</span><span class="op">/</span>
kfn	<span class="op">=</span>  <span class="opc">int</span>(256 <span class="op">+</span> 69 <span class="op">+</span> 0.5 <span class="op">+</span> 12 <span class="op">*</span> <span class="opc">log</span>(kcps <span class="op">/</span> 440) <span class="op">/</span> <span class="opc">log</span>(2))
<span class="op">/</span><span class="op">*</span> <span class="opc">grain</span> duration <span class="op">*</span><span class="op">/</span>
kgdur	<span class="opc">port</span> 100, 0.1, 20
kgdur	<span class="op">=</span>  kgdur <span class="op">/</span> kcps

a1	<span class="opc">grain2</span> kcps, kcps <span class="op">*</span> 0.02, kgdur, 50, kfn, 2, <span class="op">-</span>0.5, 22, 2
a1	<span class="opc">butterlp</span> a1, 3000
a2	<span class="opc">grain2</span> kcps, kcps <span class="op">*</span> 0.02, 4 <span class="op">/</span> kcps, 50, kfn, 2, <span class="op">-</span>0.5, 23, 2
a2	<span class="opc">butterbp</span> a2, 12000, 8000
a2	<span class="opc">butterbp</span> a2, 12000, 8000
aenv1	<span class="opc">linseg</span> 0, 0.01, 1, 1, 1
aenv2	<span class="opc">linseg</span> 3, 0.05, 1, 1, 1
aenv3	<span class="opc">linseg</span> 1, p3 <span class="op">-</span> 0.2, 1, 0.07, 0, 1, 0

a1	<span class="op">=</span>  aenv1 <span class="op">*</span> aenv3 <span class="op">*</span> (a1 <span class="op">+</span> a2 <span class="op">*</span> 0.7 <span class="op">*</span> aenv2)

ga01	<span class="op">=</span>  ga01 <span class="op">+</span> a1 <span class="op">*</span> 10000 <span class="op">*</span> iamp

	<span class="oblock">endin</span>

<span class="op">/</span><span class="op">*</span> output <span class="oblock">instr</span> <span class="op">*</span><span class="op">/</span>

	<span class="oblock">instr</span> 81

i1	<span class="op">=</span>  0.000001
aLl, aLh, aRl, aRh	<span class="opc">spat3di</span> ga01 <span class="op">+</span> i1<span class="op">*</span>i1<span class="op">*</span>i1<span class="op">*</span>i1, 3.0, 4.0, 0.0, 0.5, 7, 4
ga01	<span class="op">=</span>  0
aLl	<span class="opc">butterlp</span> aLl, 800.0
aRl	<span class="opc">butterlp</span> aRl, 800.0

	<span class="opc">outs</span> aLl <span class="op">+</span> aLh, aRl <span class="op">+</span> aRh

	<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="stamnt">t</span> 0 60

<span class="stamnt">i</span> 1 0.0 1.3 60 127
<span class="stamnt">i</span> 1 2.0 1.3 67 127
<span class="stamnt">i</span> 1 4.0 1.3 64 112
<span class="stamnt">i</span> 1 4.0 1.3 72 112

<span class="stamnt">i</span> 81 0 6.4

<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
      </div>
      <div class="refsect1">
        <a id="idp113406000"></a>
        <h2>See Also</h2>
        <p>
      <a class="link" href="grain3.html" title="grain3"><em class="citetitle">grain3</em></a>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp113408112"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Istvan Varga</td>
          </tr>
        </table>
        <p>
    </p>
        <p>New in version 4.15</p>
        <p>Updated April 2002 by Istvan Varga</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="grain.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="grain3.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">grain </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> grain3</td>
        </tr>
      </table>
    </div>
  </body>
</html>
